using System.Text;
using Klear.Library.Helpers;
using Klear.Library.Models;

namespace Klear.Library.Services;
public class ChineseAlmanacService : IChineseAlmanacService
{
    private AlmanacModel _almanacModel=new AlmanacModel();
    
    public async Task<AlmanacModel> RunAsync()
    {
        DatabaseService databaseService = new DatabaseService();
        
        using (HttpClient client = new HttpClient())
        {
            HttpContent content = new StringContent(AccessKeyStorage.REQBODY, Encoding.UTF8, "application/x-www-form-urlencoded");
            try
            {
                HttpResponseMessage response = await client.PostAsync(AccessKeyStorage.URL, content);
                response.EnsureSuccessStatusCode();

                string responseBody = await response.Content.ReadAsStringAsync();
                _almanacModel=JsonHelper.JsonToAlmanac(responseBody);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception occurred: " + ex.Message);
            }
            return _almanacModel;
        }
    }
}